﻿''' <summary>
''' インスタンスユニーク文字列生成クラス
''' </summary>
''' <remarks>キー項目が同じであればインスタンスが別でも同列の文字列を返却しますので注意</remarks>
Public Class ModelKeyFactory

    Public Shared Function Create(model As Object) As String
        Dim key As String = String.Empty

        For Each item In (From el In model.GetType.ToTable.Columns Where el.IsLoadKey Order By el.PropertyInfo.Name)
            If String.IsNullOrEmpty(key) = False Then key &= ","
            key &= item.GetValue(model)
        Next

        Return String.Format("{0}({1})", model.GetType.FullName, key)
    End Function

    Public Shared Function Create(t As Type, cols As IEnumerable(Of String), ByVal row As Dictionary(Of String, Object)) As String
        Dim key As String = String.Empty

        For Each col In cols
            'キーの列を出力していない場合、キー文字列はありません
            If row.ContainsKey(col) = False Then Return Nothing

            Dim val As Object = row.Item(col)

            'キーの値がない場合、キー文字列はありません
            If IsDBNull(val) Then Return Nothing

            If String.IsNullOrEmpty(key) = False Then key &= ","
            key &= val
        Next

        Return String.Format("{0}({1})", t.FullName, key)
    End Function

End Class
